package com.rtx.edi.platform.ediapi.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rtx.edi.gatway.handle.GatWayHandle;
import com.rtx.edi.model.response.EdiResponse;
import com.rtx.edi.platform.ediapi.bean.EdiApi;
import com.rtx.edi.platform.ediapi.service.EdiApiService;
import com.rtx.edi.platform.rule.bean.PlSerialRule;
import com.rtx.edi.platform.rule.service.PlSerialRuleService;
import com.rtx.edi.platform.tableconfig.bean.EdiColumnValueConfig;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@SuppressWarnings("all")
@Api(tags = {"Edi接口"})
@RestController
@RequestMapping("/v1/ediApi")
public class EdiApiController {
	
	@Autowired
	EdiApiService ediApiService;
	
	
	@Autowired
	ApplicationContext context;
	
	@Autowired
	GatWayHandle gatWayHandle;
	
	@Autowired
	PlSerialRuleService serialRuleService;
	
    @ApiOperation(value = "获取EDI配置自定义逻辑", notes = "获取EDI自定义逻辑springbean配置自定义逻辑")
    @PostMapping("/queryConfigEdiCustomProcess")
    public EdiResponse queryConfigEdiCustomProcess(@RequestBody EdiApi dto){
    	Map<String, Object> map =	ediApiService.queryConfigEdiCustomProcess(dto);
        return EdiResponse.success().data("item", map);
    }
    
    @ApiOperation(value = "配置自定义逻辑", notes = "配置自定义逻辑")
    @PostMapping("/configEdiCustomProcess")
    public EdiResponse configEdiCustomProcess(@RequestBody String requestStr){
    	try {
    		ediApiService.configEdiCustomProcess(requestStr);
    		return EdiResponse.success();
		} catch (Exception e) {
			return EdiResponse.failed().returnMgr(e.getMessage());
		}
    }
	
    /**
     * 获取wms标准Edi开放接口创建配置
     * @return
     */
    @ApiOperation(value = "分页获取Edi开放接口创建配置", notes = "分页获取Edi开放接口创建配置")
    @PostMapping("selectEdiApiPageList")
    public EdiResponse selectEdiApiPageList(@RequestBody EdiApi dto){
        Page<EdiApi> pageParam = new Page<>(dto.getPageNum(), dto.getPageSize());
        ediApiService.selectList(pageParam,dto);
        return EdiResponse.success().data("page",pageParam);
    }

    /**
     * Edi开放接口创建配置
     * @return
     */
    @ApiOperation(value = "Edi开放接口创建配置", notes = "根据IDEdi开放接口创建配置")
    @PostMapping("selectEdiApiById")
    public EdiResponse selectEdiApiById(@RequestBody EdiApi dto){
        return EdiResponse.success().data("item",ediApiService.getById(dto.getId()));
    }

    /**
     * 新增Edi开放接口创建配置
     * @return
     */
    @ApiOperation(value = "新增Edi开放接口创建配置", notes = "新增Edi开放接口创建配置")
    @PostMapping("addEdiApi")
    public EdiResponse addEdiApi(@RequestBody EdiApi dto){
    	try {
    		
//    		ediApiService.save(dto);
    		ediApiService.addAndInitEdiApi(dto);
            return EdiResponse.success();
		} catch (Exception e) {
			return EdiResponse.failed().returnMgr(e.getMessage());
		}
        
    }

    /**
     * 修改Edi开放接口创建配置
     * @return
     */
    @ApiOperation(value = "修改Edi开放接口创建配置", notes = "根据ID修改Edi开放接口创建配置")
    @PostMapping("editEdiApi")
    public EdiResponse editEdiApi(@RequestBody EdiApi dto){
    	ediApiService.updateById(dto);
        return  EdiResponse.success();
    }

//    /**
//     * 删除Edi开放接口创建配置
//     * @return
//     */
//    @ApiOperation(value = "删除修改Edi开放接口创建配置", notes = "根据ID删除修改Edi开放接口创建配置")
//    @PostMapping("deleteEdiApi")
//    public EdiResponse deleteEdiApi@RequestBody EdiApi dto){
//        FieldValidateUtils.fieldNotNull(dto,FieldValidateType.DELETE);
//        return  ediApiService.removeById(dto.getId()) ? EdiResponse.success() : EdiResponse.failed();
//    }

    /**
     * 批量删除Edi开放接口创建配置
     * @return
     */
    @ApiOperation(value = "批量删除Edi开放接口创建配置", notes = "批量删除Edi开放接口创建配置")
    @PostMapping("deleteEdiApiIds")
    public EdiResponse deleteTargetFieldConvertRuleByIds(@RequestBody List<EdiApi> list){
        List<Long> ids = list.stream().map(p -> p.getId()).distinct().collect(Collectors.toList());
        return  ediApiService.removeBatchByIds(ids) ? EdiResponse.success() : EdiResponse.failed();
    }
    
    /**
     * 初始化表名称与字段配置
     */
    @ApiOperation(value = "初始化表字段与报文配置", notes = "初始化表字段与报文配置")
    @PostMapping("initTableValueConfig")
    public EdiResponse intTableValueConfig(@RequestBody EdiApi dto) {
    	return null;
    }
    
    @ApiOperation(value = "查询流水号规则", notes = "查询流水号规则")
    @PostMapping("queryserialrule")
    public EdiResponse table(@RequestBody EdiApi dto){
		List<PlSerialRule> ruleList = serialRuleService.list();
		List<Map<String, String>> sSelector = new ArrayList<>();
		Map<String,String> temp = new HashMap<String, String>();
		temp.put("label", "");
		temp.put("value", "");
		sSelector.add(temp);
		
		for(PlSerialRule rule:ruleList) {
			Map<String,String> map = new HashMap<String, String>();
			map.put("label", rule.getName());
			map.put("value", rule.getId()+"");
			sSelector.add(map);
		}
        return EdiResponse.success().data("item", sSelector);
    }
    
    @ApiOperation(value = "queryActiveWhseid", notes = "获取EDI数据源")
    @PostMapping("/queryActiveWhseid")
    public EdiResponse queryActiveWhseid(@RequestBody EdiApi dto){
    	
    	String[] names = context.getBeanNamesForType(DataSource.class);
    	
    	List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		Map<String,String> datasource = new HashMap<String, String>();
		datasource.put("label", "");
		datasource.put("value", "");
		list.add(datasource);
		
		for(String name:names) {
			Map<String,String> datasourceTemp = new HashMap<String, String>();
			if("ris_edi".equals(name)) {
				datasourceTemp.put("label", "edi平台");
			}else if("enterprise".equals(name)) {
				datasourceTemp.put("label", "企业级");
			}else if(name.startsWith("wmwhse")){
				datasourceTemp.put("label", name.replace("wmwhse", "仓库"));
			}else {
				continue;
			}
			
			datasourceTemp.put("value", name);
			list.add(datasourceTemp);
		}
		
        return EdiResponse.success().data("item", list);
    }
    
    @ApiOperation(value = "queryWhseidRuel", notes = "获取EDI仓库确定规则")
    @PostMapping("/queryWhseidRuel")
    public EdiResponse queryWhseidRuel(@RequestBody EdiApi dto){
    	List<Map<String,Object>> list =	ediApiService.queryWhseidRuel();
        return EdiResponse.success().data("item", list);
    }
    
    @ApiOperation(value = "queryUpdRule", notes = "获取更新数据规则")
    @PostMapping("/queryUpdRule")
    public EdiResponse queryUpdRule(@RequestBody EdiApi dto){
    	List<Map<String,Object>> list =	ediApiService.queryUpdRuel();
    	return EdiResponse.success().data("item", list);
    }
    
    @ApiOperation(value = "apitest", notes = "获取EDI仓库确定规则")
    @PostMapping("/apitest")
    public EdiResponse apitest(@RequestBody String testrequest){
        return EdiResponse.success().data("testapirespose", ediApiService.apitest(testrequest));
    }
    
    //将对应缓存删除
    @ApiOperation(value = "刷新缓存", notes = "刷新缓存")
    @PostMapping("/refreshCaceh")
    public EdiResponse refreshCaceh(@RequestBody EdiApi dto){
    	gatWayHandle.cache.remove(dto.getCode());
        return EdiResponse.success();
    }
}
